﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Microsoft.SharePoint.Administration;
using System.Web.UI.WebControls;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web.UI;

namespace zevenseas.SharePoint.Solutions.FeatureBlocker.ApplicationPages
{
    public class Form : LayoutsPageBase
    {
        protected InputFormTextBox uxReason;
        protected Label uxFeatureDescription;
        protected Label uxFeatureTitle;
        protected Label LabelErrorMessage;
        protected Label LabelMessage;

        private SPFeatureScope scope;

        protected override void CreateChildControls()
        {
            string featureGuid = this.Context.Request.QueryString["Id"];
            if (!string.IsNullOrEmpty(featureGuid))
            { GetFeature(featureGuid); }

            base.CreateChildControls();
        }

        public void GetFeature(string featureGuid)
        {
            SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions[new Guid(featureGuid)];
            uxFeatureTitle.Text = feature.GetTitle(CultureInfo.CurrentUICulture).ToString();
            uxFeatureDescription.Text = feature.GetDescription(CultureInfo.CurrentUICulture).ToString();
            scope = feature.Scope;
        }

        public string GetListUrl()
        {
            string listUrl = string.Empty;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPWebApplication webApplication = SPWebApplication.Lookup(SPContext.Current.Site.WebApplication.GetResponseUri(SPUrlZone.Default));
                FeatureBlockerConfig config = webApplication.GetChild<FeatureBlockerConfig>("FeatureBlockerConfig");
                if (config != null)
                {
                    listUrl = config.listUrl;
                }
            });

            return listUrl;
        }

        public void SaveListItem(string listUrl)
        {
            using (SPSite site = new SPSite(listUrl))
            {
                site.AllowUnsafeUpdates = true;

                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = null;
                    try
                    {
                        list = web.GetList(listUrl);
                    }
                    catch (Exception) { }
                    if (list == null)
                    {

                    }

                    SPListItem listItem = list.Items.Add();
                    listItem["Title"] = uxFeatureTitle.Text.ToString();
                    listItem["Reason"] = uxReason.Text.ToString();
                    listItem["Web"] = this.Web.Title.ToString();
                    listItem["Url"] = this.Web.Url.ToString();

                    listItem.Update();

                    web.AllowUnsafeUpdates = false;
                }

                site.AllowUnsafeUpdates = false;
            }
        }


        public void OnClickOK(object sender, EventArgs e)
        {
            bool listItemAdded = false;

            try
            {
                SaveListItem(GetListUrl());
                listItemAdded = true;
            }
            catch (Exception error)
            {
                LabelErrorMessage.Text = "Your request could not be added.";
                LabelErrorMessage.Text += error.Message.ToString();
            }

            if (listItemAdded)
            {
                string previousPageUrl = "/_layouts/managefeatures.aspx?Scope=";

                switch (scope)
                {
                    case SPFeatureScope.Web:
                        previousPageUrl += "Web";
                        break;
                    case SPFeatureScope.Site:
                        previousPageUrl += "Site";
                        break;
                }
                SPUtility.TransferToSuccessPage("Your request will be processed, you will be contacted as soon as possible",
                    previousPageUrl, "", "");

            }
        }
    }
}
